<?php
class PicUploadAction extends CommonAction {
	public function index() {
		$this->display();
	}
	public function upload() {
		if (!empty($_FILES['img_url'])) {
			$file = $_FILES['img_url'];
			//检查大小
			if ($file['size'] >= 2 * 1024 * 1024) {
				$this->error('文件大小不能超过2M！');
				exit();
			}
			//检查类型
			if (!preg_match('/^image/', $file['type'])) {
				$this->error('只允许上传图片文件！');
			}
			$filetype = end(explode('.', $file['name']));
			//文件转存
			$filename = md5(uniqid() . $file['name']) . '.' . $filetype;
			$webroot  = dirname(dirname(dirname(dirname(__FILE__))));
			$filepath = '/Public/uploadFiles/' . date('Y-m-d') . '/';
			if (!file_exists($webroot . $filepath)) {
				mkdir($webroot . $filepath, 0755);
			}
			$fk = move_uploaded_file($file['tmp_name'], $webroot . $filepath . $filename);
			$this->assign('file', $file);
			$this->assign('path', $filepath . $filename);
		}
		$this->display();
	}
}
?>